package com.eshopkeeper.core.util.yunzhi;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串工具类
 * @author chen
 *
 */
public class StringUtil {
	
	/**
	 * 判断字符串不为空
	 * @param source
	 * @return
	 */
	public static boolean isNotEmpty(String source){
		
		if(source != null && !"".equals(source) && !source.equalsIgnoreCase("null")){
			
			return true;
			
		}else{
			
			return false;
		}
		
	}
	
	/**
	 * 判断字符串为空
	 * @param source
	 * @return
	 */
	public static boolean isEmpty(String source){
		
		return !isNotEmpty(source);
		
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public static List stringToArray(String str){
		List result =new ArrayList();
		String[] type = str.split(";");
		for (int i = 0; i < type.length; i++) {
			String[] temp = type[i].split("\\|");
			String[] name = new String[5];
			for (int j = 0; j < temp.length; j++) {
				name[j] = temp[j];
			}
			result.add(name);
		}
		
		return result;
	}
	
	public static void main(String[] args){
		List list = stringToArray("A|B|C|D|E;1|2|3|4|5");
		
		String[] a1 = (String[]) list.get(0);
		String[] a2 = (String[]) list.get(1);
		Map<String, String> map = new HashMap<String, String>();
		int i = 0;
		
		while (i < a1.length) {
			map.put(a1[i], a2[i]);
			i++;
		}

	}

	public static String find(String str,String regexpFind,String regexpFindFind){
		String[] all=findAll(str, regexpFind);
		for(String temp:all){
			str=find(temp,regexpFindFind);
			if(str!=null){
				return str;
			}
		}
		return null;
	}

	/**
	 * 根据正则表达式在字符串中查找子串
	 * @param str
	 * @param regexp
	 * @return
	 */
	public static String find(String str,String regexp){
		Pattern p=Pattern.compile(regexp);
		Matcher m=p.matcher(str);
		if(m.find()){
			return m.group();
		}
		return null;
	}

	public static String[] findAll(String str,String regexp){
		Pattern p=Pattern.compile(regexp);
		Matcher m=p.matcher(str);
		List<String> list=new ArrayList<String>();
		while(m.find()){
			list.add(m.group());
		}
		return list.toArray(new String[list.size()]);
	}
	
}
